-- Thanks: https://github.com/sathishmanohar/quick-buffer-jump
local M = {}

local strings = require('plenary.strings')
local color = require('utils.color')
local t = require('utils.table')
local misc = require('utils.misc')

local fnamemodify = vim.fn.fnamemodify

-- cmds is an array table of file full path
function M.quick_select(cmds)
    local api = vim.api
    local lines = {}
    local cmd_map = {}  -- Table to map line numbers to file name
    local alphabet = misc.get_alphabet()
    local alphabet_count = 1
    local alphabet_max = string.len(alphabet)

    -- Create buffer for the floating window
    local float_buf = api.nvim_create_buf(false, true)

    t.foreach(cmds,function(cmd)
        if cmd ~= "" then
            if alphabet_count < alphabet_max then
                local alphabet_label = alphabet:sub(alphabet_count, alphabet_count)
                if string.sub(cmd, 1, 1) == "-" or cmd == "" then
                    local label = ' ' .. ' ' .. cmd
                    t.append(lines,label)
                else
                    local label = alphabet_label .. ' ' .. cmd
                    t.append(lines,label)
                end
                cmd_map[#lines] = cmd

                -- Map the alphabet key to the cmd
                local map_cmd = "<cmd>lua vim.api.nvim_win_close(vim.fn.bufwinid(" .. tostring(float_buf) .. "), true);vim.cmd(' " .. cmd .. "')<CR>"
                api.nvim_buf_set_keymap(float_buf, 'n', alphabet_label, map_cmd, {noremap = true, silent = true})

                alphabet_count = (alphabet_count % #alphabet) + 1
            end
        else
           t.append(lines,'  ' .. cmd)
           cmd_map[#lines] = cmd
        end
    end)

    --local max_height = math.max(1, math.min(#lines, math.floor(vim.o.lines * 0.8)))
    local max_height = #lines + 2
    local width = 70
    local row = (vim.o.lines - max_height) / 2
    local col = (vim.o.columns - width) / 2

    -- Create the floating window
    local float_win = api.nvim_open_win(float_buf, true, {
		--title = cwd,
        --title_pos = "right",
        relative = 'editor',
        width = width,
        height = max_height,
        row = row,
        col = col,
        style = 'minimal',
        border = 'single'
    })

    api.nvim_set_option_value('cursorline', true, {win=float_win})

    -- display title
    local cwd = fnamemodify(vim.fn.getcwd(), ":~")
    local w = strings.strdisplaywidth(cwd)
    local s = string.rep(' ',width-w)
    api.nvim_buf_set_lines(float_buf, 0, -1, true, { s .. cwd })

    -- display content
    api.nvim_buf_set_lines(float_buf, 1, -1, false, lines)

    -- Apply highlights for each alphabet label
    t.foreach(lines,function(_,i)
        local start_pos = 0
        local end_pos = 1
        api.nvim_buf_add_highlight(float_buf, -1, 'AlphabetLabel', i, start_pos, end_pos)
    end)

    -- Store the file map in a buffer-local variable
    api.nvim_buf_set_var(float_buf, "cmd_map", cmd_map)

    -- Key mapping for Enter to switch to the file of the current line
    vim.api.nvim_create_user_command('QuickCmdSelectorEnter', function()
        local cmd = vim.api.nvim_buf_get_var(float_buf,'cmd_map')[vim.api.nvim_win_get_cursor(0)[1]-1]
        if not cmd then
            return
        end -- title
        vim.api.nvim_win_close(vim.fn.bufwinid(float_buf), true)
        vim.cmd(cmd)
    end, {})

    api.nvim_buf_set_keymap(float_buf, 'n', '<CR>', "<cmd>QuickCmdSelectorEnter<CR>", {noremap = true, silent = true})

end

return M
-- EOP
